package com.stt.experiment.trynetty.buffer;

import com.stt.experiment.trynetty.util.UnicodeUtils;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

/**
 * @author yjy
 * 2018-05-25 15:28
 */
@Getter
@Setter
@ToString(callSuper = true)
public abstract class AbstractResponseBuffer extends AbstractBuffer {

    private static final long serialVersionUID = 732795329075519220L;

    public static final int STATUS_FAILED = 0; // 处理失败
    public static final int STATUS_SUCCESS = 1;  // 处理成功

    protected Integer status; // 处理结果
    protected String info; // 错误信息
    protected Integer code; // 错误码
    protected String dataString; // 消息

    public AbstractResponseBuffer() {}

    public AbstractResponseBuffer(AbstractBuffer buffer, String dataString) {
        this(buffer, STATUS_SUCCESS, null, null, dataString);
    }

    public AbstractResponseBuffer(AbstractBuffer buffer, String info, Integer code) {
        this(buffer, STATUS_FAILED, info, code, null);
    }

    public AbstractResponseBuffer(AbstractBuffer buffer, String info, Integer code, String dataString) {
        this(buffer, STATUS_FAILED, info, code, dataString);
    }

    public AbstractResponseBuffer(AbstractBuffer buffer, int status, String info, Integer code, String dataString) {
        this (buffer.module, buffer.cmd, status, info, code, dataString);
    }

    public AbstractResponseBuffer(String module, String cmd, int status, String info, Integer code, String dataString) {
        this.module = module;
        this.cmd = cmd;
        this.status = status;
        this.info = info;
        this.code = code;
        this.dataString = dataString;
    }

    @Override
    public String toJsonString() {
        return "{\"module\":\"" + module + "\","
                + "\"cmd\":\"" + cmd + "\","
                + "\"status\":" + status + ","
                + "\"data\":" + dataString + ","
                + "\"info\":\""
                + (info == null ? "" : UnicodeUtils.toEncodedUnicode(info.trim(), false)) + "\","
                + "\"code\":" + code + "}";
    }

}
